package com.example.filerwdemo;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class MainActivity extends AppCompatActivity {
    private EditText edtName,edtPwd;
    private  CheckBox checkBox;
    MyDBHelper myDBHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       initView();
        myDBHelper=new MyDBHelper(this);

    }
    private void initView()
    {
        checkBox= (CheckBox) findViewById(R.id.chkRemPwd);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("savePwd",checkBox.isChecked());
                editor.commit();
            }
        });
        SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
        Boolean bl=sp.getBoolean("savaPwd",false);
        checkBox.setChecked(bl);



        edtName= (EditText) findViewById(R.id.edtName);
        edtPwd= (EditText) findViewById(R.id.edtPwd);
        findViewById(R.id.btnLogin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strName,strPwd;
                strName=edtName.getText().toString();
                strPwd=edtPwd.getText().toString();
                if(strName.equals("") || strPwd.equals(""))
                {
                    Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (checkBox.isChecked())
                {
                  /*  Map<String,String> rec= new HashMap<String, String>();
                    rec.put("name",strName);
                    rec.put("pwd",strPwd);
                    FileSavePwd.saveInfo(MainActivity.this,rec);*/
                  myDBHelper.insert(strName,strPwd);

                }

                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            }
        });

   /*     Map<String,String> info=FileSavePwd.readInfo(this);
        edtName.setText(info.get("name"));
        edtPwd.setText(info.get("pwd"));*/
        if(checkBox.isChecked())
        {
            List<Map<String,String>> list=myDBHelper.query();
            edtName.setText(list.get(0).get("name"));
            edtPwd.setText(list.get(0).get("pwd"));
        }
    }



}
